// pages/store/store.js
const app = getApp();
let CONSTANT = require('../../common/constant.js');
let util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0, 
    items: [
      { name: "花茶", type: "scented_tea" }, 
      { name: "普洱茶", type: "pu_er_tea" }, { name: "白茶", type:"white_tea" }, 
      { name: "红茶", type: "black_tea" }, { name: "黑茶", type:"dark_green_tea" }, 
      { name: "乌龙茶", type: "oo_long" }, { name: "绿茶", type:"green_tea" }, 
      { name: "保健茶", type:"health_protection_tea"}],
    tea: [{ oneSlideShowUrl: '../../images/testImg.png', productName: "伴手礼系列-古树颠红120g", primeCost: "48" }, 
    { oneSlideShowUrl: '../../images/testImg.png', productName: "伴手礼系列-古树颠红120g", primeCost: "48" }],
    showLoading:false,
    pageIndex:1,
    pageSize:2,
    moreData:true
  },
  // 获取容器高度，使页面滚动到容器底部
  pageScrollToBottom: function () {
    wx.createSelectorQuery().select('#j_page').boundingClientRect(function (rect) {
      // 使页面滚动到底部
      wx.pageScrollTo({
        scrollTop: rect.bottom
      })
    }).exec()
  },

  toDetail(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../detail/detail?id='+id,
    })
  },
  onChange(e){
    console.log(e);
    let index = e.detail.index;
    app.globalData.teaType = index;
    this.setData({
      active:index,
      pageIndex:1,
      moreData:true,
    })
    this.getProducts(this.data.pageIndex,this.data.pageSize)
      .then(res=>{
        this.setData({
          tea:res.data.bussData,
        })
      })
      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // const this.getProducts();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.setData({
        active:app.globalData.teaType
      })
      this.data.pageIndex=0
    this.getProducts(this.data.pageIndex, this.data.pageSize)
      .then(res => {
        this.setData({
          tea: res.data.bussData,
          pageIndex:this.data.pageIndex+1
        })
      })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.moreData) return;
    let pgIndex = this.data.pageIndex;
    let pgSize = this.data.pageSize;
    this.setData({
      showLoading: true
    });
    this.getProducts(pgIndex, pgSize)
      .then(res=>{
        console.log(res);
        let data = res.data.bussData;
        if(data.length ==0){
          wx.showToast({
            title: '已经到底了~',
            icon:'none'
          }),
          this.setData({
            moreData:false,
            showLoading:false
          })
          return;
        }
        let tea = [...this.data.tea,...data];
        console.log(tea);
        this.setData({
          tea,
        })
      });
    
  },
 getProducts(pageIndex,pageSize){

    return new Promise((resolve,reject)=>{
      util.post({
        url: CONSTANT.URL.LIST.GETPRODUCTS,
        data: {
          pageIndex: pageIndex,
          pageSize: pageSize,
          productType: this.data.items[this.data.active].type
        },
        success: (res) => {
          this.setData({
            pageIndex: this.data.pageIndex + 1,
          })
          resolve(res);
        },
        fail: (err) => {
          reject(err)
        }
      })
    })
  }
})